package jkim.plugin.daframe.ext.sql.preferences.page;

import jkim.plugin.daframe.ext.sql.DaframeSQLLogger;
import jkim.plugin.daframe.ext.sql.preferences.PreferenceConstants;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class DaframeSQLPreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	IntegerFieldEditor sqlNumField = null;
	public DaframeSQLPreferencePage() {
		super(GRID);
	}

	public void createFieldEditors() {
		StringFieldEditor encodingEditor = new StringFieldEditor(PreferenceConstants.DAFRAME_PREFERENCE_ENCODING,
				"(&E)" + DaframeSQLLogger.getString("daframe.preference.page.label.encoding"), getFieldEditorParent());
		encodingEditor.setPreferenceStore(DaframeSQLLogger.getDefault().getPreferenceStore());
		addField(encodingEditor);
		addField(new BooleanFieldEditor(PreferenceConstants.P_BOOLEAN,
				"&An example of a boolean preference", getFieldEditorParent()));
		addField(new RadioGroupFieldEditor(PreferenceConstants.P_CHOICE, 
				"An example of a multiple-choice preference", 1,
				new String[][] { { "&Choice 1", "choice1" }, { "C&hoice 2", "choice2" } }, getFieldEditorParent()));
		this.sqlNumField = new IntegerFieldEditor(PreferenceConstants.DAFRAME_PREFERENCE_SQL_NUM,
				"(&T)" + DaframeSQLLogger.getString("daframe.preference.page.label.sqlnum"), getFieldEditorParent());
		addField(this.sqlNumField);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
		setPreferenceStore(DaframeSQLLogger.getDefault().getPreferenceStore());
		setDescription(DaframeSQLLogger.getString("daframe.preference.desc.text"));
	}

	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event) {
		// TODO Auto-generated method stub
		super.propertyChange(event);
		if (event.getProperty().equals(FieldEditor.VALUE)) {
			// checkState();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#checkState()
	 */
	@Override
	protected void checkState() {
		super.checkState();

		int sqlNum = sqlNumField.getIntValue();
		if (sqlNum > 10 && sqlNum < 1000) {
			setErrorMessage(null);
			setValid(true);
		} else {
			setErrorMessage(DaframeSQLLogger.getString("daframe.preference.page.check.sqlnum"));
			setValid(false);
		}
	}

}